﻿using System;
using System.Collections.Generic;
using System.Linq;
using ET;
using Marx;
using Server;

namespace Demo
{
    public class AccountLocation
    {
        public long Id;
        public string Name;

        public int GateId;
        public int LogicId;
        public long PlayerId;
    }


    public class PlayerService : ServiceBase, ITick
    {

        private Dictionary<long, AccountLocation> accounts_;
        private List<StartSceneConfig> gateConfigs_;
        private List<StartSceneConfig> logicConfigs_;

        protected override void OnInit()
        {
            base.OnInit();
            int zone = this.DomainZone();
            accounts_ = new Dictionary<long, AccountLocation>();
            gateConfigs_ = StartConfig.Instance.Scenes.Where(i => i.Type == MarxSceneType.Gate && i.ZoneId == zone).ToList();
            logicConfigs_ = StartConfig.Instance.Scenes.Where(i => i.Type == ServerSceneType.Logic && i.ZoneId == zone).ToList();
        }

        public void Tick()
        {

        }

        public void GetAccountLocation(SSGetClientLocationRequest req, SSGetClientLocationResponse resp)
        {
            var account = accounts_.GetOrDefault(req.AccountId);
            if (account == null)
            {
                int n = RandomHelper.RandomNumber(0, gateConfigs_.Count);
                var gateId = gateConfigs_[n].ID;

                int m = RandomHelper.RandomNumber(0, logicConfigs_.Count);
                var logicId = logicConfigs_[m].ID;

                account = new AccountLocation()
                {
                    Id = req.AccountId,
                    PlayerId = 0,
                    GateId = gateId,
                    LogicId = logicId,
                };
                accounts_.Add(account.Id, account);
                resp.GateAppId = gateId;
                resp.LogicAppId = logicId;
                return;
            }
            resp.GateAppId = account.GateId;
            resp.LogicAppId = account.LogicId;
        }

    }
}
